﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Cofe.Core.Utils
{
    /// <summary>
    /// EnumUtils for a specified type of Attribute.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static class EnumUtils<T>         
    {
        public static Object[] AddProperties()
        {
            List<object> retList = new List<object>();

            foreach (var item in Enum.GetValues(typeof(T)))
                retList.Add(item);

            return retList.ToArray();
        }

        public static T Parse(object property)
        {
            return (T)property;
        }

        public static T Parse(string property)
        {
            return (T)Enum.Parse(typeof(T), property);
        }



        

    

        /// <summary>
        /// Taken from Minon's Blog 2.0
        /// http://mironabramson.com/blog/category/C.aspx?page=2
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strType"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool EnumTryParse(string strType, out T result)
        {
            string strTypeFixed = strType.Replace(' ', '_');
            if (Enum.IsDefined(typeof(T), strTypeFixed))
            {
                result = (T)Enum.Parse(typeof(T), strTypeFixed, true);
                return true;
            }
            else
            {
                foreach (string value in Enum.GetNames(typeof(T)))
                {
                    if (value.Equals(strTypeFixed, StringComparison.OrdinalIgnoreCase))
                    {
                        result = (T)Enum.Parse(typeof(T), value);
                        return true;
                    }
                }
                result = default(T);
                return false;
            }
        }


    }
}
